class Coord {
	constructor(viewer) {
		this._viewer = viewer;
		this._scene = viewer.scene;
	}

	/**
	 * 屏幕坐标转为世界坐标
	 */
	screen2World(position, noPickEntity) {
		var cartesian;
		var pickedObject;
		try {
			pickedObject = this._scene.pick(position, 5, 5);
		} catch (e) {
			console.log("scene.pick 拾取位置时异常");
		}

		if (this._scene.pickPositionSupported && Cesium.defined(pickedObject)) {
			var pcEntity = this.hasPickedModel(pickedObject, noPickEntity);
			if (!pcEntity) {
				cartesian = this._scene.pickPosition(position);
				if (Cesium.defined(cartesian)) {
					var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
					if (cartographic.height >= 0) return cartesian;
					if (!Cesium.defined(pickedObject.id) && cartographic.height >= -500) return cartesian;
				} else {}
			} else {
			}
		} else {}

		if (this._scene.onlyPickModelPosition) return cartesian; 

		//提取鼠标点的地理坐标 
		if (this._scene.mode === Cesium.SceneMode.SCENE3D) {
			//三维模式下
			var pickRay = this._scene.camera.getPickRay(position);
			cartesian = this._scene.globe.pick(pickRay, this._scene);
		} else {
			//二维模式下
			cartesian = this._scene.camera.pickEllipsoid(position, this._scene.globe.ellipsoid);
		}

		if (Cesium.defined(cartesian) && this._scene.camera.positionCartographic.height < 10000) {
			var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
			if (cartographic.height < -5000) return null; //屏蔽无效值
		}
		return cartesian;
	}
	/**
	 * 世界坐标转为屏幕坐标
	 */
	world2Screen(cartesian3) {
		return Cesium.SceneTransforms.wgs84ToWindowCoordinates(this._scene, cartesian3);
	}
	
	/**
	 * 世界坐标转为经纬度
	 * @param {Object} position
	 */
	world2Gs(cartesian3){
		var ellipsoid=this._scene.globe.ellipsoid;
		var cartographic=ellipsoid.cartesianToCartographic(cartesian3);
		var lat=Cesium.Math.toDegrees(cartograhphic.latitude);
		var lng=Cesium.Math.toDegrees(cartograhpinc.longitude);
		var alt=cartographic.height;
		return {
			x:lng,
			y:lat,
			z:alt,
		}
	}
	
	/**
	 * 经纬度转为世界坐标
	 * @param {Object} position
	 */
	gs2World(position){
		return Cesium.Cartesian3.fromDegrees(position.x, position.y, position.z)
	}
	
	/**
	 * @description 销毁
	 */
	destroy() {
		delete this._scene
		delete this._viewer
		return Cesium.destroyObject(this);
	}

	hasPickedModel(pickedObject, noPickEntity) {
		if (Cesium.defined(pickedObject) && Cesium.defined(noPickEntity)) {
			if (Cesium.defined(noPickEntity.id) && Cesium.defined(pickedObject.id)) {
				if (Cesium.defined(noPickEntity.id.id) && Cesium.defined(pickedObject.id.id) && noPickEntity.id
					.id == pickedObject.id.id) {
					return true
				} else if (noPickEntity.id == pickedObject.id) {
					return true
				}
			}
		}
		return false;
	}
}
export {
	Coord
};
